package com.smartmore.test;

import java.util.ArrayList;
import java.util.HashMap;

import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Color;
import android.graphics.RectF;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView.ScaleType;

public class Image2Activity extends Activity {
    /** Called when the activity is first created. */
	
	float mx, my;
	float curX, curY;
	
	ImageGridView iv = null;
	Button bt1 = null;
	Button layer2_1 = null;
	Button layer2_2 = null;
	String[] imageFilePaths = null;
	HashMap<String,ArrayList> cacheMap = null;
	
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.image2_layout);
        
        
        
       
        final RectImageView child1 = new RectImageView(getApplicationContext());
        final Bitmap bitmap1 = BitmapFactory.decodeResource(getResources(), R.drawable.oldman);
        child1.setImageBitmap(bitmap1);
        child1.setDrawingCacheEnabled(true);
        child1.buildDrawingCache(true);
        //child1.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT));
        //child1.setLayoutParams(new LayoutParams(200,280));
        child1.setBackgroundColor(Color.RED);
        child1.setScaleType(ScaleType.MATRIX);
        
        final RectImageView child2 = new RectImageView(getApplicationContext());
        final Bitmap bitmap2 = BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher);
        child2.setImageBitmap(bitmap2);
        child2.setMinimumWidth(200);
        child2.setMinimumHeight(200);
        //child2.setBackgroundColor(Color.BLUE);
        //child2.setScaleType(ScaleType.FIT_END);
        
        final RectImageView child3 = new RectImageView(getApplicationContext());
        child3.setImageBitmap(bitmap1);
        child3.setBackgroundColor(Color.GREEN);
        child3.setScaleType(ScaleType.MATRIX);
        
        
        iv = (ImageGridView) this.findViewById(R.id.img2);
        //String[] imageFilePaths = {"/mnt/sdcard/data/0-1920-2560.jpg","/mnt/sdcard/data/1-2560-1920.jpg"};        
        //imageFilePaths = {"/mnt/sdcard/DCIM/Camera/IMG_20120327_161333.jpg","/mnt/sdcard/DCIM/Camera/IMG_20120502_185623.jpg"};
//        imageFilePaths = new String[2];
//        imageFilePaths[0] = "/mnt/sdcard/DCIM/Camera/IMG_20120327_161333.jpg";
//        imageFilePaths[0] = "/mnt/sdcard/DCIM/Camera/5.jpg";
//        imageFilePaths[1] = "/mnt/sdcard/DCIM/Camera/IMG_20120502_185623.jpg";
//        imageFilePaths[1] = "/mnt/sdcard/DCIM/pudding.to/pudto_Americano_20120118_234843.jpg";

        imageFilePaths = new String[4];
        imageFilePaths[0] = "/mnt/sdcard/DCIM/Camera/IMG_20120327_161333.jpg";
        imageFilePaths[1] = "/mnt/sdcard/DCIM/Camera/5.jpg";
        imageFilePaths[2] = "/mnt/sdcard/DCIM/Camera/IMG_20120502_185623.jpg";
        //imageFilePaths[3] = "/mnt/sdcard/DCIM/Camera/minjae.jpg";
        imageFilePaths[3] = "/mnt/sdcard/DCIM/pudding.to/pudto_Americano_20120118_234843.jpg";
        
        
        ArrayList<RectImageView> rectImageViewList = RectImageViewFactory.createRectImageViewList(getApplicationContext(), 1, imageFilePaths);        
        for(int i=0; i<rectImageViewList.size(); i++) {
        	iv.addView(rectImageViewList.get(i));
        }
        
    	cacheMap = new HashMap<String, ArrayList>();
        
        layer2_1 = (Button)findViewById(R.id.bt_layer_2_1);
        layer2_2 = (Button)findViewById(R.id.bt_layer_2_2);
        layer2_1.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				
				if(iv.getChildCount() > 0) {
//					ArrayList<RectImageView> cacheList = new ArrayList<RectImageView>();
//					for(int i=0; i<2; i++) {
//						cacheList.add((RectImageView)iv.getChildAt(i));
//					}
//					cacheMap.put("2_2",cacheList);
					iv.removeAllViews();
				}

//				ArrayList<RectImageView> rectImageViewList = null;
//				if(cacheMap.containsKey("2_1")) {
//					rectImageViewList = cacheMap.get("2_1");
//				}else {
//					rectImageViewList = RectImageViewFactory.createRectImageViewList(getApplicationContext(), 1, imageFilePaths);
//					cacheMap.put("2_1",rectImageViewList);		
//				}				
//				iv.removeAllViews();
				
				ArrayList<RectImageView> rectImageViewList = null;
				rectImageViewList = RectImageViewFactory.createRectImageViewList(getApplicationContext(), 1, imageFilePaths);
		        for(int i=0; i<rectImageViewList.size(); i++) {
		        	iv.addView(rectImageViewList.get(i));
		        }
				
				
				
				
			}
		});
        
        layer2_2.setOnClickListener(new View.OnClickListener() {			
			@Override
			public void onClick(View v) {
				
				if(iv.getChildCount() > 0) {
//					ArrayList<RectImageView> cacheList = new ArrayList<RectImageView>();
//					for(int i=0; i<2; i++) {
//						cacheList.add((RectImageView)iv.getChildAt(i));
//					}
//					cacheMap.put("2_1",cacheList);
					iv.removeAllViews();
				}
				
//				ArrayList<RectImageView> rectImageViewList = null;
//				if(cacheMap.containsKey("2_2")) {
//					rectImageViewList = cacheMap.get("2_2");
//				}else {
//					rectImageViewList = RectImageViewFactory.createRectImageViewList(getApplicationContext(), 2, imageFilePaths);							
//				}
//				
//				iv.removeAllViews();
				
				ArrayList<RectImageView> rectImageViewList = null;
				rectImageViewList = RectImageViewFactory.createRectImageViewList(getApplicationContext(), 2, imageFilePaths);
		        for(int i=0; i<rectImageViewList.size(); i++) {
		        	iv.addView(rectImageViewList.get(i));
		        }
				
					
			}
		});
        
        /*
        iv.addView(child2);
        iv.addView(child1);
        iv.addView(child3);
        */
        
        bt1 = (Button)findViewById(R.id.bt_1);
        bt1.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				View view = iv.getChildAt(0);
				RectImageView rectImageView = (RectImageView)view;
				RectF rectF = rectImageView.getBitmapRect();

				Log.d("bitmap",rectF.left+","+rectF.top+","+rectF.width()+","+rectF.height());
				
//				ImageProcLib.makeCropImage(imageFilePaths[0], 90,
//						(int)rectF.height(),
//                        (int)rectF.width(),
//                        (int)rectF.width(),
//                        (int)rectF.height(), (int)rectF.left, (int)rectF.top,
//                        "/mnt/sdcard/data/"+System.currentTimeMillis()+".jpg");

//				Rect outRect = new Rect();
//				child1.getDrawingRect(outRect);
//				int scrollX = ((int)child1.mScroollX < 0)?0:(int)child1.mScroollX;
//				int scrollY = ((int)child1.mScroollY < 0)?0:(int)child1.mScroollY;
//				
//				Log.d("scrollXY", "==="+(int)child1.mScroollX+","+(int)child1.mScroollY+",width=>"+child1.getWidth()+",height=>"+child1.getHeight());
//				int x = ((int)child1.mScroollX <=0 )?0:(int)child1.mScroollX;
//				int y = ((int)child1.mScroollY <=0 )?0:(int)child1.mScroollY;
//				
//				RectF rectF = child1.getBitmapRect();
//				Log.d("bitmap",rectF.left+","+rectF.top+","+rectF.width()+","+rectF.height());
//				
//				
//				Bitmap child1Bitmap = child1.getDrawingCache();
//
//				 Matrix matrix = new Matrix();
//				 float currentScale = child1.getCurrentScaleFactor();
//				 float currentFocusX		= child1.getCurrentScaleDetectorFocusX();
//				 float currentFocusY		= child1.getCurrentScaleDetectorFocusY();
//
//				 matrix.postScale(currentScale, currentScale);
//
//			     int width = (int)(200/currentScale);
//			     int height = (int)(280/currentScale);
//			     int rx 	= (int)(((x)/currentScale));
//			     int ry     = (int)((y/currentScale));
//			     int termx 	= (int)((width*currentScale)) ;
//			     int termy 	= (int)((height*currentScale)) ;
//			     
//			     
//
//			     x = Math.abs((int)rectF.left);
//			     y = Math.abs((int)rectF.top);
//
//			     Bitmap resizedBitmap = Bitmap.createBitmap(bitmap1, 0, 0, 1280,1280 ,child1.mSuppMatrix, true);
//			     resizedBitmap = Bitmap.createBitmap(resizedBitmap,x,y,200,280);
//			     child3.setImageBitmap(resizedBitmap);
			}
		});
       
    }

}
